package com.oocl.scan.handler;

import com.oocl.scan.model.FieldElement;
import com.oocl.scan.model.JavaClassElement;
import com.oocl.scan.model.Relationship;
import com.oocl.scan.store.GlobalStore;
import com.oocl.scan.utils.ComponentRelationUtil;
import com.oocl.scan.utils.ScanConstants;
import java.util.Iterator;
import java.util.List;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ExpressionStatement;
import org.eclipse.jdt.core.dom.IfStatement;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.QualifiedName;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.SimpleType;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.TryStatement;

public class ConstantVariableHandler extends BaseHandler implements Handler {

  private String fromModule;
  private String from;

  @Override
  public void handle(JavaClassElement javaClassElement) {
//  System.out.println("----" + javaClassElement.getClassName());
    List<MethodDeclaration> list = javaClassElement.getMethodDeclarationList();
    fromModule = javaClassElement.getModuleName();
    for (MethodDeclaration methodDeclaration : list) {
      if (methodDeclaration.getBody() != null) {
//      System.out.println("--" + methodDeclaration.getName());
        from = javaClassElement.getJavaClassFullName() + "-" + methodDeclaration.getName();
        visitBlock(methodDeclaration.getBody());
      }
    }
  }

  private void visitBlock(Block block) {
    List<Statement> statementList = block.statements();
    if (!statementList.isEmpty()) {
      for (Statement statement : statementList) {
        visitStatement(statement);
      }
    }
  }

  private void visitStatement(Statement statement) {
    if (statement instanceof ReturnStatement) {
      return;
    } else if (statement instanceof IfStatement) {
      Statement thenStatement = ((IfStatement) statement).getThenStatement();
      if (thenStatement instanceof Block) {
        Block thenBlock = (Block) thenStatement;
        visitBlock(thenBlock);
      }
    } else if (statement instanceof TryStatement) {
      TryStatement tryStatement = (TryStatement) statement;
      visitBlock(tryStatement.getBody());
    } else if (statement instanceof ExpressionStatement) {
      ExpressionStatement expressStatement = (ExpressionStatement) statement;
      if (expressStatement.getExpression() instanceof MethodInvocation) {
//      System.out.println(expressStatement.getExpression());
        visitMethodInvocation((MethodInvocation) expressStatement.getExpression());
      }
    }
  }

  private void visitMethodInvocation(MethodInvocation methodInvocation) {
    List argumentList = methodInvocation.arguments();
    Iterator iterator = argumentList.iterator();
    while (iterator.hasNext()) {
      Object object = iterator.next();
      if (object instanceof QualifiedName) {
        QualifiedName qualifiedName = (QualifiedName) object;
//      System.out.println(GlobalStore.getInstance().getFieldElementMap().size());
        FieldElement fieldElement = GlobalStore.getInstance().getFieldElementMap().get(qualifiedName.getQualifier().toString() + " " + qualifiedName.getName());
        if (fieldElement != null && isFulfillQueueTopicEVGRules(fieldElement.getValue())) {
          Relationship relationship = ComponentRelationUtil
              .buildRelationship(ScanConstants.RELATION_TYPE_FLOWS, null, from, fromModule, fieldElement.getValue().replaceAll("\"", ""), "STRING_CONSTANTS");
          addToRelationshipStore(relationship);
        }
      } else if (object instanceof SimpleType) {
        if (GlobalStore.getInstance().getFieldElementMap().containsKey(object.toString())) {
          System.out.println(GlobalStore.getInstance().getFieldElementMap().get(object.toString()));
        }
      }
    }
  }


  private boolean isFulfillQueueTopicEVGRules(String str) {
    if (str == null) {
      return false;
    }
    if (str.replaceAll("\"", "").trim().toUpperCase().endsWith("EVT") || str.replaceAll("\"", "").trim().toUpperCase().startsWith("IR4.") &&
        (str.contains(".") || str.contains("_"))) {
      return true;
    }
    return false;
  }
}
